// #include <stdbool.h>
// #include <stdio.h>
// #include <stdlib.h>
//
// typedef struct Header {
//     int length;
//     struct Header *next;
// }pHead;
//
// typedef struct Node {
//     int data;
//     struct Node *next;
// }LNode;
//
// pHead * createList() {
//     pHead * ph;
//     ph = (pHead *) malloc(sizeof(pHead));
//     ph -> length = 0;
//     ph->next = NULL;
// }

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

// 定义单链表结构体
typedef struct Head{
    struct Node* head; // 头指针，指向链表的第一个节点
    int length; // 链表的长度
} LinkedList;

// 定义单链表的节点结构体
typedef struct Node {
    int data;
    struct Node * next;
} Node;


// 创建新节点
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 初始化单链表
void initList(LinkedList* list) {
    // LinkedList* newList = (LinkedList*)malloc(sizeof(LinkedList));
    list->head = NULL;
    list->length = 0;
}

int isListEmpty(LinkedList* list) {
    return list->head == NULL;
}

// 销毁单链表
void destroyList(LinkedList* list) {
    Node* current = list->head;
    while (current != NULL) {
        Node* next = current->next;
        free(current);
        current = next;
    }
    list->head = NULL;
    list->length = 0;
}

// 查找值
int findValue(LinkedList* list, int value) {
    Node* current = list->head;
    int position = 0;
    while (current != NULL) {
        if (current->data == value) {
            return position;
        }
        current = current->next;
        position++;
    }
    return -1; // 未找到
}

// 任意位置插入值
bool insertValue(LinkedList* list, int value, int position) {
    if (position < 0 || position > list->length) {
        printf("Invalid position.\n");
        return false;
    }
    Node* newNode = createNode(value);
    if (position == 0) { // 插入到头部
        newNode->next = list->head;
        list->head = newNode;
    } else {
        Node* current = list->head;
        for (int i = 0; i < position - 1; i++) {
            current = current->next;
        }
        newNode->next = current->next;
        current->next = newNode;
    }
    list->length++;
    return true;
}

// 删除值
void deleteValue(LinkedList* list, int value) {
    Node* current = list->head;
    Node* previous = NULL;
    while (current != NULL && current->data != value) {
        previous = current;
        current = current->next;
    }
    if (current == NULL) {
        printf("Value not found.\n");
        return;
    }
    if (previous == NULL) { // 删除头节点
        list->head = current->next;
    } else {
        previous->next = current->next;
    }
    free(current);
    list->length--;
}

// 打印所有值
void printList(LinkedList* list) {
    Node* current = list->head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    LinkedList list;
    initList(&list);

    insertValue(&list, 10, 0);
    insertValue(&list, 20, 1);
    insertValue(&list, 30, 2);
    printf("List after insertions: ");
    printList(&list);

    int position = findValue(&list, 20);
    if (position != -1) {
        printf("Value 20 found at position %d\n", position);
    } else {
        printf("Value 20 not found.\n");
    }

    deleteValue(&list, 20);
    printf("List after deleting 20: ");
    printList(&list);

    destroyList(&list);
    return 0;
}